package savet.gui.tabelas;

import javax.swing.event.InternalFrameEvent;
import savet.entidades.Empregador;
import savet.gui.controllers.GuiSAVETController;
import savet.gui.util.IUtilTabela;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JTabbedPane;
import javax.swing.event.InternalFrameAdapter;
import org.openswing.swing.mdi.client.InternalFrame;
import savet.entidades.Trabalhador;
import savet.gui.tabelas.group.NossoListSelectionModel;
import savet.gui.util.GeradorTabelasUtil;
import savet.util.JOptionUtil;
import savet.util.ListaTrabalhadores;
import savet.util.LoteUtil;
import savet.util.SauditaLog;
import savet.util.Trabalhadores;

/**
 *
 * @author Rafael
 */
public class GuiSAVET extends InternalFrame implements IUtilTabela {

    private List<Trabalhador> trabalhadores = ListaTrabalhadores.getInstance().getTrabalhadores();
    private GuiSAVETController guiSAVETController;
    JScrollBar bar = new JScrollBar();

    /** Creates new form GuiSAVET */
    public GuiSAVET(GuiSAVETController guiSAVETController) {
        this.guiSAVETController = guiSAVETController;

        initComponents();

        gerarTabelasBasicas();

        jScrollPane0.setVerticalScrollBar(bar);
        jScrollPaneRegistro.setVerticalScrollBar(bar);

        NossoListSelectionModel listSelectionModel = new NossoListSelectionModel();
        listSelectionModel.setUtilTabela(this);
        jTableVinculos.setSelectionModel(listSelectionModel);
        jTableRescisao.setSelectionModel(listSelectionModel);
        jTableFerias.setSelectionModel(listSelectionModel);
        jTableRaisCaged.setSelectionModel(listSelectionModel);
        jTableRegistro.setSelectionModel(listSelectionModel);
        jTableExamMed.setSelectionModel(listSelectionModel);
        this.guiSAVETController.setUtilTabela(this);

        /**
         * Captura data do empregador atual.
         */
        LoteUtil loteUtil = LoteUtil.getInstance();
        Empregador empregadorAtual = loteUtil.getEmpregadorAtual();
        if (empregadorAtual != null) {

            Date inicioPeriodoNotificado = empregadorAtual.getInicioPeriodoNotificado();
            Date fimPeriodoNotificado = empregadorAtual.getFimPeriodoNotificado();

            if (inicioPeriodoNotificado != null) {
                inicioPeriodo.setDate(inicioPeriodoNotificado);
            }
            if (fimPeriodoNotificado != null) {
                fimPeriodo.setDate(fimPeriodoNotificado);
            }

            gerarTabelasDatas();
        }

        addInternalFrameListener(new InternalFrameAdapter() {

            @Override
            public void internalFrameClosing(InternalFrameEvent e) {
                LoteUtil.getInstance().resetarContadorAuditoria();
                ListaTrabalhadores.getInstance().setTrabalhadores(new LinkedList<Trabalhador>());
            }
        });
    }

    public void gerarTabelasBasicas() {
        gerarTabelasVinculos();
        gerarTabelaRegistro();
        gerarTabelasSalarioAtrivutos();
    }

    public void gerarTabelasDatas() {
        try {
            carregarDatasPeriodoNotificado();
            gerarTabelaRaisCaged();
            gerarTabelaFerias();
            gerarTabelaRescisao();
            gerarTabelaExamMed();
        } catch (NullPointerException e) {
            SauditaLog.adicionarSevereLog(GuiSAVET.class, e.getMessage(), e);
        }
    }

    private void carregarDatasPeriodoNotificado() throws NullPointerException {
        dataInicio = inicioPeriodo.getDate();
        //new GregorianCalendar(2009, 0, 1).getTime();
        dataFim = fimPeriodo.getDate();
        //new GregorianCalendar(2011, 11, 1).getTime();

        if (dataInicio == null || dataFim == null) {
            throw new NullPointerException("Data(s) do periodo notificado nao informada(s)!");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dataInicio = new java.util.Date();
        dataFim = new java.util.Date();
        jPanel1 = new javax.swing.JPanel();
        importButton = new org.openswing.swing.client.ImportButton();
        insertButton = new org.openswing.swing.client.InsertButton();
        deleteButton = new org.openswing.swing.client.DeleteButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane0 = new javax.swing.JScrollPane();
        jTableVinculos = new savet.gui.tabelas.group.JTableGroupableHeader();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanelRegistro = new javax.swing.JPanel();
        jScrollPaneRegistro = new javax.swing.JScrollPane();
        jTableRegistro = new savet.gui.tabelas.group.JTableGroupableHeader();
        jPanelRaisCaged = new javax.swing.JPanel();
        jScrollPaneRaisCaged = new javax.swing.JScrollPane();
        jTableRaisCaged = new savet.gui.tabelas.group.JTableGroupableHeader();
        jPanelFerias = new javax.swing.JPanel();
        jScrollPaneFerias = new javax.swing.JScrollPane();
        jTableFerias = new savet.gui.tabelas.group.JTableGroupableHeader(){

        };
        jPanelRescisao = new javax.swing.JPanel();
        jScrollPaneRescisao = new javax.swing.JScrollPane();
        jTableRescisao = new savet.gui.tabelas.group.JTableGroupableHeader();
        jPanelExamMed = new javax.swing.JPanel();
        jScrollPaneExamMed = new javax.swing.JScrollPane();
        jTableExamMed = new savet.gui.tabelas.group.JTableGroupableHeader();
        jPanelSalarioAtributos = new javax.swing.JPanel();
        jScrollPaneSalarioAtributos = new javax.swing.JScrollPane();
        jTableSalarioAtributos = new savet.gui.tabelas.group.JTableGroupableHeader();
        jPanel9 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jPanel11 = new javax.swing.JPanel();
        okPeriodo = new javax.swing.JButton();
        inicioPeriodo = new com.toedter.calendar.JDateChooser("dd/MM/yyyy", "##/##/####", '_');
        fimPeriodo = new com.toedter.calendar.JDateChooser("dd/MM/yyyy", "##/##/####", '_');

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Vinculos");
        setAutoscrolls(true);
        setPreferredSize(new java.awt.Dimension(1356, 600));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        importButton.setAttributeName(" ");
        importButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importButtonActionPerformed(evt);
            }
        });

        insertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertButtonActionPerformed(evt);
            }
        });

        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(importButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(insertButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(1082, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(insertButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(importButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(34, 34, 34))
        );

        importButton.setText("Importar");
        importButton.setFocusable(true);

        jLabel1.setFont(new java.awt.Font("Arial", 0, 18));
        jLabel1.setText("Período Notificado de :");

        jLabel2.setFont(new java.awt.Font("Arial", 0, 18));
        jLabel2.setText("até:");

        jPanel3.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel3.setPreferredSize(new java.awt.Dimension(535, 404));

        jScrollPane0.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        jScrollPane0.setPreferredSize(new java.awt.Dimension(452, 520));

        jTableVinculos.setBackground(new java.awt.Color(235, 235, 235));
        jTableVinculos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jTableVinculos.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jTableVinculos.setIntercellSpacing(new java.awt.Dimension(0, 0));
        jTableVinculos.setRowHeight(20);
        jTableVinculos.setSelectionBackground(new java.awt.Color(183, 217, 241));
        jTableVinculos.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jScrollPane0.setViewportView(jTableVinculos);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane0, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jScrollPane0, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE))
        );

        jSplitPane1.setLeftComponent(jPanel3);

        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPane1StateChanged(evt);
            }
        });

        jScrollPaneRegistro.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        jTableRegistro.setBackground(new java.awt.Color(235, 235, 235));
        jTableRegistro.setModel(new javax.swing.table.DefaultTableModel());
        jTableRegistro.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTableRegistro.setIntercellSpacing(new java.awt.Dimension(0, 0));
        jTableRegistro.setRowHeight(20);
        jTableRegistro.setSelectionBackground(new java.awt.Color(183, 217, 241));
        jTableRegistro.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jScrollPaneRegistro.setViewportView(jTableRegistro);

        javax.swing.GroupLayout jPanelRegistroLayout = new javax.swing.GroupLayout(jPanelRegistro);
        jPanelRegistro.setLayout(jPanelRegistroLayout);
        jPanelRegistroLayout.setHorizontalGroup(
            jPanelRegistroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneRegistro, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
        );
        jPanelRegistroLayout.setVerticalGroup(
            jPanelRegistroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneRegistro, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("REGISTRO", jPanelRegistro);

        jScrollPaneRaisCaged.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        jTableRaisCaged.setBackground(new java.awt.Color(235, 235, 235));
        jTableRaisCaged.setModel(new javax.swing.table.DefaultTableModel());
        jTableRaisCaged.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTableRaisCaged.setIntercellSpacing(new java.awt.Dimension(0, 0));
        jTableRaisCaged.setRowHeight(20);
        jTableRaisCaged.setSelectionBackground(new java.awt.Color(183, 217, 241));
        jTableRaisCaged.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jScrollPaneRaisCaged.setViewportView(jTableRaisCaged);

        javax.swing.GroupLayout jPanelRaisCagedLayout = new javax.swing.GroupLayout(jPanelRaisCaged);
        jPanelRaisCaged.setLayout(jPanelRaisCagedLayout);
        jPanelRaisCagedLayout.setHorizontalGroup(
            jPanelRaisCagedLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneRaisCaged, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
        );
        jPanelRaisCagedLayout.setVerticalGroup(
            jPanelRaisCagedLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneRaisCaged, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("RAIS/CAGED", jPanelRaisCaged);

        jScrollPaneFerias.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        jTableFerias.setBackground(new java.awt.Color(235, 235, 235));
        jTableFerias.setModel(new javax.swing.table.DefaultTableModel());
        jTableFerias.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTableFerias.setIntercellSpacing(new java.awt.Dimension(0, 0));
        jTableFerias.setRowHeight(20);
        jTableFerias.setSelectionBackground(new java.awt.Color(183, 217, 241));
        jTableFerias.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jScrollPaneFerias.setViewportView(jTableFerias);

        javax.swing.GroupLayout jPanelFeriasLayout = new javax.swing.GroupLayout(jPanelFerias);
        jPanelFerias.setLayout(jPanelFeriasLayout);
        jPanelFeriasLayout.setHorizontalGroup(
            jPanelFeriasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneFerias, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
        );
        jPanelFeriasLayout.setVerticalGroup(
            jPanelFeriasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneFerias, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("FÉRIAS", jPanelFerias);

        jScrollPaneRescisao.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        jTableRescisao.setBackground(new java.awt.Color(235, 235, 235));
        jTableRescisao.setModel(new javax.swing.table.DefaultTableModel());
        jTableRescisao.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTableRescisao.setIntercellSpacing(new java.awt.Dimension(0, 0));
        jTableRescisao.setRowHeight(20);
        jTableRescisao.setSelectionBackground(new java.awt.Color(183, 217, 241));
        jTableRescisao.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jScrollPaneRescisao.setViewportView(jTableRescisao);

        javax.swing.GroupLayout jPanelRescisaoLayout = new javax.swing.GroupLayout(jPanelRescisao);
        jPanelRescisao.setLayout(jPanelRescisaoLayout);
        jPanelRescisaoLayout.setHorizontalGroup(
            jPanelRescisaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneRescisao, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
        );
        jPanelRescisaoLayout.setVerticalGroup(
            jPanelRescisaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneRescisao, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("RESCISÃO", jPanelRescisao);

        jScrollPaneExamMed.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        jTableExamMed.setBackground(new java.awt.Color(235, 235, 235));
        jTableExamMed.setModel(new javax.swing.table.DefaultTableModel());
        jTableExamMed.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTableExamMed.setIntercellSpacing(new java.awt.Dimension(0, 0));
        jTableExamMed.setRowHeight(20);
        jTableExamMed.setSelectionBackground(new java.awt.Color(183, 217, 241));
        jTableExamMed.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jScrollPaneExamMed.setViewportView(jTableExamMed);

        javax.swing.GroupLayout jPanelExamMedLayout = new javax.swing.GroupLayout(jPanelExamMed);
        jPanelExamMed.setLayout(jPanelExamMedLayout);
        jPanelExamMedLayout.setHorizontalGroup(
            jPanelExamMedLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneExamMed, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE)
        );
        jPanelExamMedLayout.setVerticalGroup(
            jPanelExamMedLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPaneExamMed, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("EXAM. MED.", jPanelExamMed);

        jPanelSalarioAtributos.setLayout(new java.awt.BorderLayout());

        jTableSalarioAtributos.setModel(new javax.swing.table.DefaultTableModel());
        jTableSalarioAtributos.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jScrollPaneSalarioAtributos.setViewportView(jTableSalarioAtributos);

        jPanelSalarioAtributos.add(jScrollPaneSalarioAtributos, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("SALÁRIO A.", jPanelSalarioAtributos);

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 657, Short.MAX_VALUE)
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 319, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("SALÁRIO C.", jPanel9);

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 657, Short.MAX_VALUE)
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 319, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("JORNADA/DESCANSO", jPanel10);

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 657, Short.MAX_VALUE)
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 319, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("+", jPanel11);

        jSplitPane1.setRightComponent(jTabbedPane1);

        okPeriodo.setText("OK");
        okPeriodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okPeriodoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(inicioPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addGap(12, 12, 12)
                .addComponent(fimPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(okPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(667, 667, 667))
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1204, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(okPeriodo)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(inicioPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(fimPeriodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1)
                        .addComponent(jLabel2)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1220)/2, (screenSize.height-505)/2, 1220, 505);
    }// </editor-fold>//GEN-END:initComponents

    private void importButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importButtonActionPerformed
        File file = Trabalhadores.getArquivoDeFileChooser(this);
        ListaTrabalhadores listTrabalhadores = ListaTrabalhadores.getInstance();
        try {
            trabalhadores = Trabalhadores.getTrabalhadoresSimples(new FileInputStream(file));

            for (Trabalhador trabalhador : trabalhadores) {
                listTrabalhadores.adicionarTrabalhador(trabalhador);
            }

            guiSAVETController.setListaTrabalhadores(listTrabalhadores.getTrabalhadores());
        } catch (IOException ex) {
            System.err.println(ex.getMessage() + " em " + getClass());
        } catch (NullPointerException ex) {
            System.err.println(ex.getMessage() + " em " + getClass());
        }
    }//GEN-LAST:event_importButtonActionPerformed

    private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPane1StateChanged

        JTabbedPane jTabbedPane = (JTabbedPane) evt.getSource();
        System.out.println("Panel selecionado:" + jTabbedPane.getSelectedIndex());

        if (jTabbedPane.getSelectedIndex() == 0) {
            jScrollPaneRegistro.setVerticalScrollBar(bar);
        } else if (jTabbedPane.getSelectedIndex() == 1) {
            jScrollPaneRaisCaged.setVerticalScrollBar(bar);
        } else if (jTabbedPane.getSelectedIndex() == 2) {
            jScrollPaneFerias.setVerticalScrollBar(bar);
        } else if (jTabbedPane.getSelectedIndex() == 3) {
            jScrollPaneRescisao.setVerticalScrollBar(bar);
        } else if (jTabbedPane.getSelectedIndex() == 4) {
            jScrollPaneExamMed.setVerticalScrollBar(bar);
        }

    }//GEN-LAST:event_jTabbedPane1StateChanged

    private void okPeriodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okPeriodoActionPerformed
        if (trabalhadores != null) {
            try {
                carregarDatasPeriodoNotificado();
//            if (inicioPeriodo.getDate() != null && fimPeriodo.getDate() != null) {
                if (inicioPeriodo.getDate().before(fimPeriodo.getDate())) {
                    gerarTabelasDatas();
                    Empregador empregadorAtual = LoteUtil.getInstance().getEmpregadorAtual();
                    if (empregadorAtual != null) {
                        empregadorAtual.setInicioPeriodoNotificado(inicioPeriodo.getDate());
                        empregadorAtual.setFimPeriodoNotificado(fimPeriodo.getDate());
                    }
                    JOptionPane.showMessageDialog(this, "Tabelas de atributos criadas.");
                } else {
                    JOptionUtil.showErrorMessage("Verifique se o período foi adicionado corretamente! ", "Erro de data");
                }
            } catch (NullPointerException ex) {
                JOptionUtil.showErrorMessage("Verifique se o período foi adicionado corretamente! ", "Erro de data");
            }
        } else {
            JOptionUtil.showErrorMessage("Por favor, primeiro adicione a lista de vinculos. ", "Erro de vinculos");
        }
    }//GEN-LAST:event_okPeriodoActionPerformed

    private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertButtonActionPerformed
        Trabalhador trabalhador = new Trabalhador();
        ListaTrabalhadores.getInstance().adicionarTrabalhador(trabalhador);
    }//GEN-LAST:event_insertButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int result = JOptionUtil.yesNoOption("Deseja realmente remover essa linha?", "Remover...");
        if (JOptionUtil.isYesResult(result)) {
            ListaTrabalhadores list = ListaTrabalhadores.getInstance();
            int selectedRow = jTableVinculos.getSelectedRow();

            // trabalhador da linha removida!
            Trabalhador t = list.getTrabalhador(selectedRow);
            if (list.removerTrabalhador(selectedRow))
                JOptionUtil.showInfoMessage("Trabalhador " + t.getNome() + " removido!", "Sucesso...");
        }
    }//GEN-LAST:event_deleteButtonActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.util.Date dataFim;
    private java.util.Date dataInicio;
    private org.openswing.swing.client.DeleteButton deleteButton;
    private com.toedter.calendar.JDateChooser fimPeriodo;
    private org.openswing.swing.client.ImportButton importButton;
    private com.toedter.calendar.JDateChooser inicioPeriodo;
    private org.openswing.swing.client.InsertButton insertButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JPanel jPanelExamMed;
    private javax.swing.JPanel jPanelFerias;
    private javax.swing.JPanel jPanelRaisCaged;
    private javax.swing.JPanel jPanelRegistro;
    private javax.swing.JPanel jPanelRescisao;
    private javax.swing.JPanel jPanelSalarioAtributos;
    private javax.swing.JScrollPane jScrollPane0;
    private javax.swing.JScrollPane jScrollPaneExamMed;
    private javax.swing.JScrollPane jScrollPaneFerias;
    private javax.swing.JScrollPane jScrollPaneRaisCaged;
    private javax.swing.JScrollPane jScrollPaneRegistro;
    private javax.swing.JScrollPane jScrollPaneRescisao;
    private javax.swing.JScrollPane jScrollPaneSalarioAtributos;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private savet.gui.tabelas.group.JTableGroupableHeader jTableExamMed;
    private savet.gui.tabelas.group.JTableGroupableHeader jTableFerias;
    private savet.gui.tabelas.group.JTableGroupableHeader jTableRaisCaged;
    private savet.gui.tabelas.group.JTableGroupableHeader jTableRegistro;
    private savet.gui.tabelas.group.JTableGroupableHeader jTableRescisao;
    private savet.gui.tabelas.group.JTableGroupableHeader jTableSalarioAtributos;
    private savet.gui.tabelas.group.JTableGroupableHeader jTableVinculos;
    private javax.swing.JButton okPeriodo;
    // End of variables declaration//GEN-END:variables

    public List<Trabalhador> getTrabalhadores() {
        return trabalhadores;
    }

    public void setTrabalhadores(List<Trabalhador> trabalhadores) {
        this.trabalhadores = trabalhadores;
    }

    public void selectRowAllTables(int index) {
        System.out.println(index);
        jTableFerias.setRowSelectionInterval(index, index);
        jTableFerias.getSelectedRow();
    }

    public void addSelectionInterval(int index0, int index1) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

//     *
//     * Metodos de geracação de tablea
//     *
//     *
//     *
    public void gerarTabelaExamMed() {
        GeradorTabelasUtil.gerarTabelaExames(jTableExamMed, dataInicio, dataFim);
    }

    public void gerarTabelaRescisao() {
        GeradorTabelasUtil.gerarTabelaRescisao(jTableRescisao, dataInicio, dataFim);
    }

    public void gerarTabelaRaisCaged() {
        GeradorTabelasUtil.gerarTabelaRaisCAGED(jTableRaisCaged, dataInicio, dataFim);

    }

    public void gerarTabelaFerias() {
        GeradorTabelasUtil.gerarTabelaFerias(jTableFerias, dataInicio, dataFim);
    }

    public void gerarTabelaRegistro() {
        GeradorTabelasUtil.gerarTabelaRegistro(jTableRegistro);
    }

    public void gerarTabelasVinculos() {
        GeradorTabelasUtil.gerarTabelaVinculos(jTableVinculos);
    }

    public void gerarTabelasSalarioAtrivutos() {
        GeradorTabelasUtil.gerarTablesSalarioAtributos(jTableSalarioAtributos);
    }
}
